﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECCSystem.CheckClass;
using System.Text.RegularExpressions;

namespace ECCSystem
{
    public partial class frmMailCategory : Form
    {
        private BizRule.ClsMailCategory objBRMC;
		private int intMode = 0;
        public frmMailCategory()
        {
            InitializeComponent();
        }

        private void frmMailCategory_Load(object sender, EventArgs e)
        {
            try
            {
                this.BindDataGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }


        private void frmMailCategory_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form frmMain = new frmMain();
            frmMain.Show();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.CheckInputItem(txtMailCategory.Text.ToString().Trim()) == true)
                {
					int intFlg = 0;

					if (Check() == false)
					{
						return;
					}

					if (ChkAppoint.Checked)
					{
						intFlg = 1;
					}

					if (intMode == 0)
					{
						objBRMC = new ECCSystem.BizRule.ClsMailCategory();
						objBRMC.AddCategory(txtMailCategory.Text.ToString().Trim(),
											txtComment.Text.ToString().Trim(),
											txtPoint.Text.ToString().Trim(), intFlg);

					}
					else
					{
						objBRMC = new ECCSystem.BizRule.ClsMailCategory();
						objBRMC.UpdateCategory(Convert.ToString(dgList.SelectedRows[0].Cells[0].Value),
												  txtPoint.Text,
												  txtMailCategory.Text.Trim(),
												  txtComment.Text.Trim(), intFlg);
						dgList.Enabled = true;
						intMode = 0;
					}

					this.btnDelete.Enabled = true;
					this.btnUpdate.Enabled = true;
					this.dgList.Enabled = true;
                    this.txtMailCategory.Text = "";
					this.txtComment.Text = "";
					this.txtPoint.Text = "";
                    this.BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                //wether have selected row,if have not selected row, pop-up prompt
                if (dgList.CurrentRow == null)
                {
                    MessageBox.Show("データを選んでください！", "ECC Information");
                    return;
                }
                else
                {
                    if (MessageBox.Show("削除します、よろしいですか？", "ECC Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        objBRMC = new ECCSystem.BizRule.ClsMailCategory();
                        objBRMC.DeleteCategory(Convert.ToString(dgList.SelectedRows[0].Cells[0].Value));
                        this.BindDataGrid();
						txtMailCategory.Text = "";
						txtComment.Text = "";
						txtPoint.Text = "";
						ChkAppoint.Checked = false;
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        //bind data
        private void BindDataGrid()
        {
            DataSet objDS = new DataSet();
            objBRMC = new ECCSystem.BizRule.ClsMailCategory();
            objDS = (DataSet)objBRMC.GetList();
            //Don't show blank row
            objDS.Tables[0].DefaultView.AllowNew = false;
            dgList.DataSource = objDS.Tables[0];

			if (dgList.Rows.Count > 0)
			{
				this.btnDelete.Enabled = true;
				this.btnUpdate.Enabled = true;
				this.dgList.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
				txtMailCategory.Text = Convert.ToString(dgList.Rows[0].Cells["MailCategory"].Value);
				txtComment.Text = Convert.ToString(dgList.Rows[0].Cells["Category_Memo"].Value);
				txtPoint.Text = Convert.ToString(dgList.Rows[0].Cells["Point"].Value);
				if (dgList.SelectedRows[0].Cells["Category_AppointFlg"].Value != System.DBNull.Value)
				{
					ChkAppoint.Checked = Convert.ToBoolean(dgList.SelectedRows[0].Cells["Category_AppointFlg"].Value);
				}
				else
				{
					ChkAppoint.Checked = false;
				}
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdate.Enabled = false;
				this.dgList.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}
        }

        //Validate Data 
        private bool CheckInputItem(string MailCategoryName)
        {
            if (MailCategoryName == "")
            {
                MessageBox.Show("カテゴリを入力してください！", "ECC Information");
                return false;
            }
            return true;
        }

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			if (dgList.CurrentRow == null)
			{
				MessageBox.Show("データを選んでください！", "ECC Information");
				return;
			}
			dgList.Enabled = false;
			//txtMailCategory.Text = Convert.ToString(dgList.SelectedRows[0].Cells["MailCategory"].Value);
			//txtComment.Text = Convert.ToString(dgList.SelectedRows[0].Cells["Category_Memo"].Value);
			//txtPoint.Text = Convert.ToString(dgList.SelectedRows[0].Cells["Point"].Value);
			intMode = 1;

			this.btnDelete.Enabled = false;
			this.btnUpdate.Enabled = false;
			this.dgList.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;

		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.txtMailCategory.Text = "";
			this.txtComment.Text = "";
			this.txtPoint.Text = "";
			intMode = 0;

			if (dgList.Rows.Count > 0)
			{
				this.btnDelete.Enabled = true;
				this.btnUpdate.Enabled = true;
				this.dgList.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;

				dgList.Rows[0].Selected = true;

				txtMailCategory.Text = Convert.ToString(dgList.CurrentRow.Cells["MailCategory"].Value);
				txtComment.Text = Convert.ToString(dgList.CurrentRow.Cells["Category_Memo"].Value);
				txtPoint.Text = Convert.ToString(dgList.CurrentRow.Cells["Point"].Value);
				if (dgList.SelectedRows[0].Cells["Category_AppointFlg"].Value != System.DBNull.Value)
				{
					ChkAppoint.Checked = Convert.ToBoolean(dgList.SelectedRows[0].Cells["Category_AppointFlg"].Value);
				}
				else
				{
					ChkAppoint.Checked = false;
				}
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdate.Enabled = false;
				this.dgList.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}

		}

		private bool Check()
		{
			if (clsUICheck.CheckLen(txtMailCategory.Text.Trim(), 50) == false)
			{
				MessageBox.Show("カテゴリ入力長さは長すぎ！", "ECC Information");
				return false;
			}

			if (clsUICheck.CheckLen(txtComment.Text.Trim(), 50) == false)
			{
				MessageBox.Show("備考入力長さは長すぎ！", "ECC Information");
				return false;
			}

			if (txtPoint.Text.Trim() == "")
			{
				MessageBox.Show("得点入力してください！", "ECC Information");
				return false;
			}

			if (!Regex.IsMatch(txtPoint.Text.Trim(), "^\\d{0,5}([.]\\d{1,2}){0,1}$"))
			{
				MessageBox.Show("得点入力不正確!", "ECC Information");
				return false;
			}

			if (intMode == 1)
			{
				for (int i = 0; i < this.dgList.Rows.Count; i++)
				{
					if (txtMailCategory.Text.ToString().Trim() == dgList.Rows[i].Cells["MailCategory"].Value.ToString().Trim() &&
						i != dgList.CurrentRow.Index)
					{
						MessageBox.Show("カテゴリ既に存在しています!", "ECC Information");
						return false;
					}
				}

				return true;
			}

			for (int i = 0; i < this.dgList.Rows.Count; i++)
			{
				if (txtMailCategory.Text.ToString().Trim() == dgList.Rows[i].Cells["MailCategory"].Value.ToString().Trim())
				{
					MessageBox.Show("カテゴリ既に存在しています!", "ECC Information");
					return false;
				}
			}

			return true;
		}

		private void dgList_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			txtMailCategory.Text = "";
			txtComment.Text = "";
			txtPoint.Text = "";
			if (dgList.SelectedRows.Count <= 0)
			{
				return;
			}

			txtMailCategory.Text = Convert.ToString(dgList.SelectedRows[0].Cells["MailCategory"].Value);
			txtComment.Text = Convert.ToString(dgList.SelectedRows[0].Cells["Category_Memo"].Value);
			txtPoint.Text = Convert.ToString(dgList.SelectedRows[0].Cells["Point"].Value);
			if (dgList.SelectedRows[0].Cells["Category_AppointFlg"].Value != System.DBNull.Value)
			{
				ChkAppoint.Checked = Convert.ToBoolean(dgList.SelectedRows[0].Cells["Category_AppointFlg"].Value);
			}
			else
			{
				ChkAppoint.Checked = false;
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			txtMailCategory.Text = "";
			txtComment.Text = "";
			txtPoint.Text = "";

			this.btnDelete.Enabled = false;
			this.btnUpdate.Enabled = false;
			this.dgList.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;

			intMode = 0;
		}
    }
}